package com.ddwl.order.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.order.service.OrderAddressServer;
import com.ddwl.order.service.OrderInfoServer;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.order.OrderRemarkUpdaeBo;
import com.ddwl.schema.bo.order.QueryOrderListBo;
import com.ddwl.schema.bo.order.ShopConfirmOrderBo;
import com.ddwl.schema.bo.order.ShopDeliveryBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.order.OrderAddrVo;
import com.ddwl.schema.vo.order.OrderDetailVo;
import com.ddwl.schema.vo.order.OrderInfoListVo;
import com.ddwl.schema.vo.shop.ShopVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Optional;

/**
 * 类ShopOrderController.java的实现描述：TODO
 *
 * @author zach
 * @Date :2020-12-03 19:41.
 */
@Api(tags = "wbm小程序店铺商家后台操作订单")
@RestController
@RequestMapping("/wbmShop")
public class WbmShopOrderController extends BaseController {


    @Resource
    private OrderInfoServer orderInfoServer;
    @Resource
    private OrderAddressServer orderAddressServer;

    @PostMapping("/page")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("商家后台查询订单列表")
    public RestVo<PageVo<OrderInfoListVo>> wbmShopPage(@Valid @RequestBody PageBo<QueryOrderListBo> pageBo) {
        QueryOrderListBo param = Optional.ofNullable(pageBo.getParam()).orElse(new QueryOrderListBo());
        param.setShopId(Optional.ofNullable(param.getShopId()).orElse(getShopId()));
        pageBo.setParam(param);
        return RestVo.ok(orderInfoServer.getWbmShopPage(pageBo));
    }

    @GetMapping("/detail")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("商户后台查看订单详情")
    public RestVo<OrderDetailVo> wbmShopDetail(@RequestParam String id) {
        return RestVo.ok(orderInfoServer.getShopOrderDetail(null,id));
    }

    @PostMapping("/update/remark")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("商家修改订单备注")
    public RestVo updateRemark(@Valid @RequestBody OrderRemarkUpdaeBo updaeBo) {
        ShopVo shopVo = getShopVo(updaeBo.getShopId());
        updaeBo.setShopId(shopVo.getId());
        orderInfoServer.shopUpdateOrderRemark(updaeBo);
        return RestVo.ok();
    }


    @PostMapping("/wbmShopConfirmOrder")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("商家核销自提订单")
    public RestVo wbmShopConfirmOrder(@Valid @RequestBody ShopConfirmOrderBo confirmOrderBo) {
        ShopVo shopVo = getShopVo(confirmOrderBo.getShopId());
        confirmOrderBo.setShopId(shopVo.getId());
        orderInfoServer.shopConfirmOrder(confirmOrderBo);
        return RestVo.ok();
    }

    @PostMapping("/wbmShopDelivery")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("商家整单发货接口")
    public RestVo wbmShopDelivery(@Valid @RequestBody ShopDeliveryBo deliveryBo) {
        ShopVo shopVo = getShopVo(deliveryBo.getShopId());
        deliveryBo.setShopId(shopVo.getId());
        orderInfoServer.shopDelivery(deliveryBo);
        return RestVo.ok();
    }

    @GetMapping("/getOrderAddr")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("商家查询订单收货信息")
    public RestVo<OrderAddrVo> getOrderAddr(@RequestParam(name="orderId") String orderId) {
//        ShopVo shopVo = shopApi.getShopByUserId(getUserId()).getResult();
//        ParamException.isNull(shopVo,"当前用户门店不存在");
        return RestVo.ok(orderAddressServer.getShopOrderAddr(orderId,null));
    }

}
